package cn.itcast.service.impl;


import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 根据月份查询
     *
     * @param date
     * @return
     */
    @Override
    public List<Map<String, String>> getOrderSettingsByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        List<OrderSetting> orderSettingList = this.orderSettingDao.getOrderSettingsByMonth(dateBegin, dateEnd);
        List<Map<String, String>> data = orderSettingList.stream().map(orderSetting -> {
            Map<String, String> orderSettingMap = new HashMap();
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderSetting.getOrderDate());
            orderSettingMap.put("date", String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
            orderSettingMap.put("number", String.valueOf(orderSetting.getNumber()));
            orderSettingMap.put("reservations", String.valueOf(orderSetting.getReservations()));
            return orderSettingMap;
        }).collect(Collectors.toList());

        return data;
    }

    /**
     * 预约设置批量添加
     *
     * @param orderSettingList
     */
    @Override
    public void upload(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            orderSettingList.stream().forEach(orderSetting -> {
                try {
                    //检查添加的日期是否存在
                    Long count = this.orderSettingDao.findCountByOrderDate(DateUtils.parseDate2String(orderSetting.getOrderDate()));
                    if (count > 0) {
                        //存在,执行更新
                        this.orderSettingDao.updateNumberByOrderDate(orderSetting);
                    } else {
                        //不存在,执行添加
                        this.orderSettingDao.add(orderSetting);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    /**
     * 更新预约人数
     *
     * @param orderSetting
     */
    @Override
    public void updateOrderSettingNumber(OrderSetting orderSetting) {
        try {
            String dateString = DateUtils.parseDate2String(orderSetting.getOrderDate());
            Long count = this.orderSettingDao.findCountByOrderDate(dateString);
            //判断是否已经设置了
            if (count > 0) {
                //设置过了,就更新
                this.orderSettingDao.updateOrderSettingNumber(orderSetting.getNumber(),dateString);
            } else {
                //没有设置,保存到数据库
                this.orderSettingDao.add(orderSetting);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
